﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NavigatorImpl.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   The navigator implementation.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace mPassRT.Services
{
    using System;

    using mPassRT.Common;

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    /// <summary>The navigator implementation.</summary>
    public class NavigatorImpl : INavigator
    {
        #region Public Methods and Operators

        /// <summary>Navigates back.</summary>
        public void GoBack()
        {
            GetCurrentFrame().GoBack();
        }

        /// <summary>Navigates to the specified page with the specified parameter.</summary>
        /// <param name="pageType">The page type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <typeparam name="T">parameter type</typeparam>
        /// <returns>True if successful; false otherwise.</returns>
        public bool NavigateTo<T>(Type pageType, T parameter) where T : class
        {
            Frame frame = GetCurrentFrame();
            string serializedParam = SerializationUtility.SerializeToString(parameter);

            return frame.Navigate(pageType, serializedParam);
        }

        #endregion

        #region Methods

        /// <summary>Gets the current frame.</summary>
        /// <returns>The current <see cref="Frame"/>.</returns>
        private static Frame GetCurrentFrame()
        {
            return (Frame)Window.Current.Content;
        }

        #endregion
    }
}